import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AdminDepartmentApi } from '#/api/edu/base/adminDepartment';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { getDeptListCondition } from '#/api/system/dept';
import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      label: '行政部门名称',
      component: 'ApiSelect',
      rules: 'required',
      componentProps(values) {
        return {
          api: async () => {
            const data = await getDeptListCondition({
              type: 'unit',
              orgTypeLast: '1101',
            });
            return data;
          },
          labelField: 'name',
          valueField: 'id',
          childrenField: 'children',
          placeholder: '请选择教育行政部门名称',
          treeDefaultExpandAll: true,
          allowClear: true,
          onChange: (value: string, options: any) => {
            values.orgCode = options.orgCode;
            values.name = options.label;
            values.areaCode = options.areaCode;
          },
        };
      },
    },
    {
      fieldName: 'name',
      label: '行政部门名称',
      component: 'Input',
      dependencies: {
        triggerFields: ['id'],
        show: () => false,
      },
    },
    {
      fieldName: 'orgCode',
      label: '行政部门编码',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入教育行政部门编码',
      },
    },
    {
      fieldName: 'licenseNumber',
      label: '社会信用代码',
      component: 'Input',
      rules: 'creditCode',
      componentProps: {
        placeholder: '请输入统一社会信用代码',
      },
    },
    {
      fieldName: 'contact',
      label: '联系人',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'cellphone',
      label: '联系人手机',
      rules: 'mobile',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系人手机',
      },
    },
    {
      fieldName: 'phone',
      label: '联系人固定电话',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系人固定电话',
      },
    },

    {
      fieldName: 'status',
      label: '有效状态',
      rules: z.number().default(CommonStatusEnum.ENABLE),
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择有效状态',
      },
    },

    {
      fieldName: 'areaCode',
      label: '行政区划',
      component: 'ApiTreeSelect',
      rules: 'required',
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({ type: 'area' });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择行政区划',
        treeDefaultExpandAll: true,
        allowClear: true,
      },
    },
    {
      fieldName: 'mainDeptId',
      label: '主管部门',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({ type: 'unit' });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择主管部门',
        treeDefaultExpandAll: true,
        allowClear: true,
      },
    },
    {
      fieldName: 'sort',
      label: '排序号',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入排序号',
      },
    },
    {
      fieldName: 'address',
      label: '地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入地址',
      },
    },
    {
      fieldName: 'remarks',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '行政部门编码',
      field: 'orgCode',
    },
    {
      label: '行政部门名称',
      field: 'name',
    },
    {
      label: '社会信用代码',
      field: 'licenseNumber',
    },
    {
      label: '联系人',
      field: 'contact',
    },
    {
      label: '联系人手机',
      field: 'cellphone',
    },
    {
      label: '联系人固定电话',
      field: 'phone',
    },
    {
      label: '有效状态',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        });
      },
    },

    {
      label: '行政区划',
      field: 'areaName',
    },
    {
      label: '主管部门',
      field: 'mainDeptName',
    },
    {
      label: '主管单位行政区划',
      field: 'mainAreaName',
    },

    {
      label: '排序号',
      field: 'sort',
    },
    {
      label: '地址',
      field: 'address',
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'orgCode',
      label: '行政部门编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入教育行政部门编码',
      },
    },
    {
      fieldName: 'name',
      label: '行政部门名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入教育行政部门名称',
      },
    },
    {
      fieldName: 'licenseNumber',
      label: '社会信用代码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入统一社会信用代码',
      },
    },
    {
      fieldName: 'contact',
      label: '联系人',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'cellphone',
      label: '联系人手机',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入联系人手机',
      },
    },
    {
      fieldName: 'phone',
      label: '联系人固定电话',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入联系人固定电话',
      },
    },
    {
      fieldName: 'status',
      label: '有效状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择有效状态',
      },
    },
    {
      fieldName: 'areaCode',
      label: '行政区划',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({ type: 'area' });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择行政区划',
        treeDefaultExpandAll: true,
        allowClear: true,
      },
    },
    {
      fieldName: 'mainDeptId',
      label: '主管部门',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({ type: 'unit' });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择主管部门',
        treeDefaultExpandAll: true,
        allowClear: true,
      },
    },

    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<AdminDepartmentApi.AdminDepartment>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '教育行政部门id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'orgCode',
      title: '行政部门编码',
      minWidth: 100,
    },
    {
      field: 'name',
      title: '行政部门名称',
      minWidth: 200,
    },
    {
      field: 'licenseNumber',
      title: '社会信用代码',
      minWidth: 120,
    },
    {
      field: 'contact',
      title: '联系人',
      minWidth: 120,
    },
    {
      field: 'cellphone',
      title: '联系人手机',
      minWidth: 120,
    },
    {
      field: 'phone',
      title: '固定电话',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '有效状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'areaName',
      title: '行政区划',
      minWidth: 120,
    },
    {
      field: 'mainDeptName',
      title: '主管部门',
      minWidth: 120,
    },
    {
      field: 'mainAreaName',
      title: '主管部门行政区划',
      minWidth: 120,
    },

    {
      field: 'sort',
      title: '排序号',
      minWidth: 80,
    },
    {
      field: 'address',
      title: '地址',
      minWidth: 200,
    },
    {
      field: 'remarks',
      title: '备注',
      minWidth: 280,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
